package com.wson.model.clouddisk.file.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.wson.adapter.clouddisk.factory.CodiskAdapterFactory;
import com.wson.adapter.codisk.ICodiskAdapter;
import com.wson.dao.entity.proton.IProtonEntityProp;
import com.wson.model.basic.IBasicView;
import com.wson.model.basic.IMarkableField;
import com.wson.model.clouddisk.file.ICodiskFileManip;
import com.wson.model.clouddisk.file.ICodiskFileMeta;
import com.wson.model.clouddisk.file.ICodiskFileView;
import com.wson.model.entity.impl.EntityModel;
import com.wson.root.util.LoggerUtil;
import com.wson.util.throwable.BasicException;

public class CodiskFileModel extends EntityModel 
	implements ICodiskFileView, ICodiskFileManip{
	

	@IMarkableField(Attribute=ATTR_DIRECTORY, Syntax=SYNC_DIRECTORY
			, PropID=PROP_CLD, PropIndex=IProtonEntityProp.PROP_VALUEINDEX_1
			, Behavior=REPLETENCY_GENERATED
	)
	private Boolean directory;
	
	@IMarkableField(Attribute=ATTR_ACCOUNTID, Syntax=SYNC_ACCOUNTID
			, PropID=PROP_CLD, PropIndex=IProtonEntityProp.PROP_VALUEINDEX_2
			, Behavior=REPLETENCY_GENERATED
	)
	private Long accountID;	
	
	@IMarkableField(Attribute=ATTR_SIZE, Syntax=SYNC_SIZE
			, PropID=PROP_CLD, PropIndex=IProtonEntityProp.PROP_VALUEINDEX_3
			, Behavior=REPLETENCY_INPUTTEXT
	)
	private Long size;
	
	@IMarkableField(Attribute=ATTR_SHAREHREF, Syntax=SYNC_SHAREHREF
			, PropID=PROP_CLD, PropIndex=IProtonEntityProp.PROP_VALUEINDEX_7
			, Behavior=REPLETENCY_INPUTTEXT
	)
	private String shareHref;
	
	@IMarkableField(Attribute=ATTR_PATH, Syntax=SYNC_PATH
			, PropID=PROP_CLD, PropIndex=IProtonEntityProp.PROP_VALUEINDEX_8
			, Behavior=REPLETENCY_INPUTTEXT
	)
	private String path;
	@IMarkableField(Attribute=ATTR_PARENTFOLDER, Syntax=SYNC_PARENTFOLDER
			, PropID=PROP_CLD, PropIndex=IProtonEntityProp.PROP_VALUEINDEX_6
			, Behavior=REPLETENCY_INPUTTEXT
	)
	private String parentFolder;
	
	private Long volume;


	

	public String getShareHref() {
		return shareHref;
	}

	public void setShareHref(String shareHref) {
		this.shareHref = shareHref;
	}

	@Override
	public Long getMetaID(){
		return new Long(ICodiskFileMeta.META_ID);
	}
	@Override
	public Long getCategoryID() {
		return new Long(ICodiskFileMeta.META_ID);
	}
	@Override
	public String getSync() {
		return SYNC_SELF;
	}
	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public Boolean getDirectory() {
		return directory;
	}

	public void setDirectory(Boolean directory) {
		this.directory = directory;
	}

	public Long getVolume() {
		return volume;
	}

	public void setVolume(Long volume) {
		this.volume = volume;
	}

	public Long getSize() {
		return size;
	}

	public void setSize(Long size) {
		this.size = size;
	}

	public String getParentFolder() {
		return parentFolder;
	}

	public void setParentFolder(String parentFolder) {
		this.parentFolder = parentFolder;
	}

	public Long getAccountID() {
		return accountID;
	}

	public void setAccountID(Long accountID) {
		this.accountID = accountID;
	}

	public String selfProvision()throws BasicException{
		//System.out.println("selfProvision "+this.getAccountID());
		
		ICodiskAdapter adapter = CodiskAdapterFactory.getAdapter(this.getAccountID());
		try{
			return adapter.retrieveProvisionUrl(this);
		}
		catch(Exception ex){
			LoggerUtil.DEBUG(ex);
			throw new BasicException(ex);
		}
	}

	@Override
	public Collection<IBasicView> selfQuery() throws BasicException {
		if(this.getParentFolder()!=null 
				&& this.getAccountID()!=null){
			ICodiskAdapter adapter = CodiskAdapterFactory.getAdapter(this.getAccountID());
			try{
				List<ICodiskFileView> files = adapter.listfiles(this.getParentFolder());
				Collection<IBasicView> results = new ArrayList<IBasicView>();
				results.addAll(files);
				return results;
			}
			catch(Exception ex){
				LoggerUtil.DEBUG(ex);
				throw new BasicException(ex);
			}
		}
		return super.selfQuery();
	}
	
	
	
}
